import fileUtil
import fetchUtil
import dbUtil
import logUtil
import math
import time
import random
import traceback

logger = logUtil.logger
f = fileUtil.fetchRecord()
c = fileUtil.config()
def excuete():
    fetchRecord = f.load()
    if fetchRecord.ak == None:
        logger.info('今日已无可用ak，请明日再试')
        return False
    if fetchRecord.extent == None:
        logger.info('所有视野数据都是抓取完毕')
        return False
    cur_extent = fetchRecord.cur_extent if fetchRecord.cur_extent != None else fetchRecord.extent
    logger.debug('开始查询%s-%s类别的数据pageNum:%s,extent:%s' % (fetchRecord.region, fetchRecord.queryType, fetchRecord.page_num, str(cur_extent)))
    reponseData = fetchUtil.getDataByExtent(fetchRecord.queryType, fetchRecord.page_num, fetchRecord.ak, cur_extent)
    if not fetchUtil.isValidRequest(reponseData):
        logger.warning('当前ak:%s请求无效,返回数据:%s' % (fetchRecord.ak, reponseData))
        fetchRecord.ak = c.getNextAk(fetchRecord.ak)
        logger.debug('切换下一个ak值:%s' ,fetchRecord.ak)
        fetchRecord.save()
        return True
    total = reponseData['total']
    logger.info("本次查询有效,请求页码:%s数据总条数为:%s" % (fetchRecord.page_num, total))
    nextQueryExtent = None
    if fetchUtil.fetchDataIsLoss(reponseData):
        nextQueryExtent = fetchUtil.getSplitExtent(cur_extent, 2, 0)
        logger.debug('当前查询已达到最大查询数据量上线,动态缩小查询范围至:%s' % str(nextQueryExtent))
        fetchRecord.cur_extent = nextQueryExtent
        fetchRecord.save()
        return True
    results = reponseData['results']
    logger.debug('开始存入数据库,请求页码%s数据量为:%s' % (fetchRecord.page_num, len(results)))
    dbUtil.batchSaveToDb(results, fetchRecord.queryType)
    if math.ceil(total / 20) > fetchRecord.page_num + 1:
        #nextQueryExtent = cur_extent
        fetchRecord.page_num = fetchRecord.page_num + 1
        logger.debug('继续查询当前视野范围数据，设置下次查询页码%s 当前数据查询类型为:%s' % (fetchRecord.page_num, fetchRecord.queryType))
        #fetchRecord.cur_extent = nextQueryExtent
        fetchRecord.save()
        return True
    tempNextQueryExtent = [fetchRecord.extent[0], cur_extent[3], fetchRecord.extent[2], fetchRecord.extent[3]]
    fetchRecord.page_num = 0
    if tempNextQueryExtent[1] != tempNextQueryExtent[3]:
        nextQueryExtent = tempNextQueryExtent
        logger.info('当前视野范围内得所有数据都已抓取完毕,重置查询页码设置下次查询数据的视野范围:%s' % str(nextQueryExtent))
        fetchRecord.cur_extent = nextQueryExtent
        fetchRecord.save()
        return True
    nextQueryExtent = fetchRecord.extent
    nextTag = c.getNextTag(fetchRecord.queryType)
    if nextTag == None:
        logger.info('视野块extent:%s对应的所有类型数据都已抓取完毕', fetchRecord.extent)
        fetchRecord.extent = c.getNextExtent(fetchRecord.extent)
        fetchRecord.queryType = c.getFirstTag()
        logger.info('重置页码与查询类型，获取并设置下一个视野范围:%s', fetchRecord.extent)
        fetchRecord.save()
        return True
    logger.info('当前%s类型数据已全部查询完全，重置查询页码与视野范围切换查询%s类别数据' % (fetchRecord.queryType, nextTag))
    fetchRecord.queryType =  nextTag
    fetchRecord.cur_extent = nextQueryExtent
    fetchRecord.save()
    return True

def run():
    flag = True
    while flag:
        try:
            flag = excuete()
            time.sleep(random.uniform(0,2))
        except BaseException :
           # traceback.print_exc()
            logger.exception('出现未知异常！')

if __name__ == '__main__':
    run()
        
